//
//  MyOrderCell.swift
//  iModern
//
//  Created by 立雄 王 on 15/8/12.
//  Copyright (c) 2015年 com.starlooking. All rights reserved.
//

import UIKit
import ImageLoader

class MyOrderCell: TableViewCell
{

    let __avatar_image_view_tags = 60
    let __preview_image_view_tag = 11
    let __salatag_label_tag      = 61
    let __button_tag             = 62
    let __alias_label_tags       = 63
    let __price_label_tags       = 64
    let __quantity_label_tags    = 65
    let __label_tags             = 66
    let __blabel_tags            = 67

    override func awakeFromNib()
    {
        super.awakeFromNib()

        initAvatarImageView()
//        initAvatarLabel()
        initindexOfSaleTagLabel()
        initButton()
        initAliasLabel()
        initPriceLabel()
        initaLabel()
        initbLabel()
        initQuantityLabel()
    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func setData(data: NSMutableDictionary)
    {
        let order       = data
        let advertising = data["advertising"] as! [String:AnyObject]
        let contact     = advertising["contact"] as! [String:AnyObject]

        let price       = advertising["price"] as? Float
        let nub         = data["quantity"] as? Float
        let totalPrices = price! * nub!

        setAvatarImageView(contact["thumbnailURL"] as? String)
        
        setIndexOfSaleTagLabel((advertising["summary"] as? String)!)
        setQuantityLabel(String((data["quantity"] as? Int)!))
        setPriceLabel(String(stringInterpolationSegment: totalPrices))
        setAliasLabel((contact["shopName"] as? String)!)

        let review = order["review"] as? String
        if review == "null" || review == nil
        {
            setButton("未评价")
        }
        else
        {
            setButton("已评价")
        }
    }

    func setButton(buton: String)
    {
        let button = self.viewWithTag(__button_tag) as! UIButton
        button.setTitle(buton, forState: UIControlState.Normal)
    }

    func setAliasLabel(alias: String)
    {
        let label = self.viewWithTag(__alias_label_tags) as! UILabel
        label.text = alias
    }

    func setPriceLabel(price: String)
    {
        let label = self.viewWithTag(__price_label_tags) as! UILabel
        label.text = "￥ \(price)"
    }

    func setQuantityLabel(quantity: String)
    {
        let label = self.viewWithTag(__quantity_label_tags) as! UILabel
        label.text = "\(quantity)个"
    }

    func setIndexOfSaleTagLabel(alias: String)
    {
        let label = self.viewWithTag(__salatag_label_tag) as! UILabel
        label.text = alias
    }

    func setAvatarImageView(thumbnailURL: String?)
    {
        let imageView = self.viewWithTag(__avatar_image_view_tags) as! UIImageView
        if thumbnailURL != nil
        {
            let url = appDelegate.parameterManager.server + "/" + thumbnailURL!
            imageView.load(url, placeholder: nil)
        }
        else
        {
            imageView.image = UIImage(named: "Avatar-Default.png")
        }
    }

    func initAliasLabel()
    {
        var label = self.viewWithTag(__alias_label_tags) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 60, y: 10, width: 250, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(16)
            label!.tag = __alias_label_tags
            label?.text = "哥本哈根美食"
            label?.textColor = UIColor.grayColor()
            self.addSubview(label!)
        }
    }

    func initButton()
    {
        var button = self.viewWithTag(__button_tag) as? UIButton
        if (button == nil)
        {
            let frame = CGRect(x: 0, y: 90, width: 100, height: 40)
            button = UIButton(frame: frame)
            button!.setTitle("已下单", forState: UIControlState.Normal)
            button!.titleLabel!.font = UIFont.systemFontOfSize(16)
            button!.setTitleColor(UIColor.orangeColor(), forState: .Normal)
            button!.tag = __button_tag
            self.addSubview(button!)
        }
    }

    func initaLabel()
    {
        let screen = UIScreen.mainScreen().bounds
        var label  = self.viewWithTag(__label_tags) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 10, y: 95, width: screen.size.width - 20, height: 1)
            label = UILabel(frame: frame)
            label?.backgroundColor = UIColor.groupTableViewBackgroundColor()
            label!.tag = __label_tags
            self.addSubview(label!)
        }
    }

    func initbLabel()
    {
        let screen = UIScreen.mainScreen().bounds
        var label  = self.viewWithTag(__blabel_tags) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 0, y: 130, width: screen.size.width, height: 10)
            label = UILabel(frame: frame)
            label?.backgroundColor = UIColor.groupTableViewBackgroundColor()
            label!.tag = __blabel_tags
            self.addSubview(label!)
        }
    }

    func initQuantityLabel()
    {
        var label = self.viewWithTag(__quantity_label_tags) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 150, y: 60, width: 100, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(16)
            label!.tag = __quantity_label_tags
            label?.text = "1个"
            self.addSubview(label!)
        }
    }

    func initPriceLabel()
    {
        var label = self.viewWithTag(__price_label_tags) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 230, y: 60, width: 150, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(16)
            label!.tag = __price_label_tags
            label?.text = "￥500"
            self.addSubview(label!)
        }
    }

    func initindexOfSaleTagLabel()
    {
        var label = self.viewWithTag(__salatag_label_tag) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 20, y: 60, width: 150, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(16)
            label!.tag = __salatag_label_tag
            label?.text = "草莓爱心曲奇饼干"
            label?.textColor = UIColor.grayColor()
            self.addSubview(label!)
        }
    }

    func initAvatarImageView()
    {
        var imageView = self.viewWithTag(__avatar_image_view_tags) as? UIImageView
        if (imageView == nil)
        {
            let frame = CGRect(x: 10.0, y: 10.0, width: 40.0, height: 40.0)
            imageView = UIImageView(frame: frame)
            imageView!.image = UIImage(named: "Avatar-Default.png")
            imageView!.tag = __avatar_image_view_tags
            imageView!.layer.cornerRadius = 20.0
            imageView!.layer.masksToBounds = true
            imageView!.layer.borderColor = UIColor.blackColor().CGColor
            imageView!.layer.borderWidth = 1.0
            self.addSubview(imageView!)
        }
    }
}
